require('./add.css');



$(function () {

    $("#city-picker").cityPicker({
        toolbarTemplate: '<header class="bar bar-nav">\
    <button class="button button-link pull-right close-picker">确定</button>\
    <h1 class="title">选择收货地址</h1>\
    </header>'
    });

    $('#submitBtn').click(function () {
        let referer = $(this).data('referer');
        let data = getFormData();
        if (!validateForm(data)) {
            return false;
        }

        if(addid != ""){
            data.add_id=addid;
        }
        $.ajax({
            type: "post",
            url: _addAddrUrl,
            data: data,
            dataType: "json",
            success: function (res) {
                if (res.rel != 1) {
                    $.toast(res.msg);
                    return false;
                }
                if (referer!=0) {
                    location.href = _orderUrl;
                }
            }
        });
    });


});



//获取提交数据
const getFormData = () => {
    let dataArr = $('form').serializeArray();
    let data = {};
    $.each(dataArr, function (key, val) {
        data[val.name] = $.trim(val.value);
    });
    return data;
};


//表单验证
const validateForm = (data) => {


    const config = [{
            name: 'name',
            label: '收货人姓名',
            rules: (value, data) => {
                if (value == '') {
                    return {
                        validated: false,
                        msg: '收货人姓名不能为空'
                    };
                }
                return {
                    validated: true
                };
            }
        },
        {
            name: 'mobile',
            label: '手机',
            rules: (value, data) => {
                let regex = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$/;
                if (value == '') {
                    return {
                        validated: false,
                        msg: '手机不能为空'
                    };
                }
                if (!regex.test(value)) {
                    return {
                        validated: false,
                        msg: '手机格式不正确'
                    };
                }
                return {
                    validated: true
                };
            }
        }, {
            name: 'city',
            label: '收货地址',
            rules: (value, data) => {
                if (value == '') {
                    return {
                        validated: false,
                        msg: '收货地址不能为空'
                    };
                }
                return {
                    validated: true
                };
            }
        },
        {
            name: 'addrss',
            label: '详细地址',
            rules: (value, data) => {
                if (value == '') {
                    return {
                        validated: false,
                        msg: '详细地址不能为空'
                    };
                }
                return {
                    validated: true
                };
            }
        }
    ];


    let checked = true;
    $.each(config, function (key, val) {
        let field = val.name;
        let res = val.rules(data[field], data);
        if (!res.validated) {
            checked = false;
            $.toast(res.msg);
            return false;
        }
    });
    if (!checked) {
        return false;
    }
    return true;
};